package com.crayfish.quduo.ui.activity;

import android.support.v7.widget.Toolbar;
import android.widget.ImageView;
import android.widget.TextView;

import com.crayfish.quduo.R;
import com.crayfish.quduo.base.BaseMvpActivity;
import com.crayfish.quduo.commom.Params;
import com.crayfish.quduo.model.JokeItemBean;
import com.crayfish.quduo.model.Test;
import com.crayfish.quduo.presenter.HotManagerPresenter;
import com.crayfish.quduo.presenter.contract.HotManagerContract;
import com.crayfish.quduo.utils.DBManager;
import com.crayfish.quduo.utils.ImageUtil;
import com.crayfish.quduo.utils.SystemUtil;
import com.crayfish.quduo.utils.ToastUtil;

import java.util.List;

import butterknife.BindView;

/**
 * Created by crayfish on 2017/1/22.
 */
public class ImageActivity extends BaseMvpActivity<HotManagerPresenter> implements HotManagerContract.View{

    @BindView(R.id.toolbar)
    Toolbar toolbar;
    @BindView(R.id.image)
    ImageView imageView;
    @BindView(R.id.title)
    TextView txt_title;
    @BindView(R.id.txt_eye)
    TextView txt_eye;
    @BindView(R.id.txt_good)
    TextView txt_good;
    @BindView(R.id.txt_comment)
    TextView txt_comment;

    JokeItemBean.Item jItem;

    @Override
    protected int getLayoutId() {
        return R.layout.activity_image;
    }

    @Override
    protected HotManagerPresenter createPresenter() {
        return new HotManagerPresenter();
    }

    @Override
    protected void initEventAndData() {
        jItem = (JokeItemBean.Item)getIntent().getSerializableExtra(Params.JOKE_ITEM);
        if(jItem.getComment() == 0) {
            txt_eye.setText("1000");
        }else{
            txt_eye.setText(String.valueOf(jItem.getComment()));
        }
        if(jItem.getLaud() == 0) {
            txt_good.setText("500");
        }else {
            txt_good.setText(String.valueOf(jItem.getLaud()));
        }
        txt_comment.setText("1.0K");
        txt_title.setText(jItem.getTitle());
        setSupportActionBar(toolbar);
        getSupportActionBar().setDisplayHomeAsUpEnabled(true);
        ImageUtil.imageGifLoad(this, jItem.getImg(), imageView);
        mPresenter.queryHot(jItem);
    }

    @Override
    public void showError(String msg) {
        ToastUtil.show(msg);
    }

    @Override
    public void searchSuccess(JokeItemBean.Item jItem) {
        DBManager.getInstance(this).insertItem(Test.toTest(jItem));
        if(SystemUtil.isEmpty(jItem.getObjectId())){
            mPresenter.addHot(jItem);
        }else{
            mPresenter.updateHot(jItem);
        }
    }
}
